﻿//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Data;

namespace DotNet.Business
{
    using DotNet.DbUtilities;
    using DotNet.Model;
    using DotNet.Utilities;
    using DotNet.Manager;

    /// <summary>
    /// BaseImpersonationManager
    /// 扮演
    /// 
    /// 修改纪录
    ///
    ///     2008.04.17 版本：1.0 JiRiGaLa 创建主键。
    ///     
    /// 版本：1.0
    /// 
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2008.04.17</date>
    /// </author>
    /// </summary>
    public class BaseImpersonationManager : BaseManager
    {
        public BaseImpersonationManager()
        {
            base.CurrentTableName = BaseStaffTable.TableName;
        }

        public BaseImpersonationManager(IDbHelper dbHelper): this()
        {
            DbHelper = dbHelper;
        }

        public BaseImpersonationManager(BaseUserInfo userInfo): this()
        {
            UserInfo = userInfo;
        }

        public BaseImpersonationManager(IDbHelper dbHelper, BaseUserInfo userInfo) : this(dbHelper)
        {
            UserInfo = userInfo;
        }

        #region public DataTable GetImpersonationList(string organizeId) 获取扮演列表
        /// <summary>
        /// 获取扮演列表
        /// </summary>
        /// <param name="organizeId">组织机构主键</param>
        /// <returns>数据表</returns>
        public DataTable GetImpersonationList(string organizeId)
        {
            return this.GetImpersonationList(organizeId, string.Empty);
        }
        #endregion

        #region public DataTable GetImpersonationList(string organizeId, string searchValue) 获取扮演列表
        /// <summary>
        /// 获取扮演列表
        /// </summary>
        /// <param name="organizeId">组织机构主键</param>
        /// <param name="search">查询</param>
        /// <returns>数据表</returns>
        public DataTable GetImpersonationList(string organizeId, string searchValue)
        {
            if (UserInfo.IsAdministrator)
            {
                BaseStaffManager staffManager = new BaseStaffManager(DbHelper, UserInfo);
                return staffManager.Search(organizeId, searchValue, false);
            }
            searchValue = BaseBusinessLogic.GetSearchString(searchValue);
            string sqlQuery = " SELECT " + BaseStaffTable.TableName + ".* "
                            + "," + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldFullName + " AS DepartmentFullName "
                            + "," + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " AS DutyFullName "
                            + "," + "OT.RoleFullName "
                            + " FROM " + BaseStaffTable.TableName
                            + "      LEFT OUTER JOIN " + BaseOrganizeTable.TableName + " ON " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldId + " = " + BaseStaffTable.FieldDepartmentId
                            + "      LEFT OUTER JOIN " + BaseItemDetailsTable.TableName + " ON  " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldId + " = " + BaseStaffTable.FieldDutyId
                            + "      LEFT OUTER JOIN " + "(SELECT " + BaseUserTable.TableName + "." + BaseUserTable.FieldId
                                                                + "," + BaseRoleTable.TableName + "." + BaseRoleTable.FieldRealname + " AS RoleFullName "
                                                         + " FROM " + BaseUserTable.TableName + "," + BaseRoleTable.TableName
                                                         + " WHERE " + BaseUserTable.TableName + "." + BaseUserTable.FieldRoleId + " = " + BaseRoleTable.TableName + "." + BaseRoleTable.FieldId + ") OT "
                            + "                  ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldId + " = OT.ID "
                            + " WHERE (" + BaseStaffTable.TableName + ".ID <> '" + UserInfo.Id + "')";
            if (!String.IsNullOrEmpty(organizeId))
            {
                sqlQuery += " AND (" + BaseStaffTable.FieldCompanyId + " = '" + organizeId + "'";
                sqlQuery += " OR " + BaseStaffTable.FieldDepartmentId + " = '" + organizeId + "')";
            }
            if (!String.IsNullOrEmpty(searchValue))
            {
                sqlQuery += " AND (" + BaseStaffTable.FieldUsername + " LIKE '" + searchValue + "'";
                sqlQuery += " OR " + BaseStaffTable.FieldRealname + " LIKE '" + searchValue + "'";
                sqlQuery += " OR " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldFullName + " LIKE '" + searchValue + "'";
                sqlQuery += " OR " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " LIKE '" + searchValue + "'";
                sqlQuery += " OR OT.RoleFullName LIKE '" + searchValue + "')";
            }
            // 获得允许扮演的范围
            if (!UserInfo.IsAdministrator)
            {
                sqlQuery += " AND " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldId + " IN (SELECT " + BaseDelegationTable.FieldConsignerId
                                                              + " FROM " + BaseDelegationTable.TableName
                                                              + " WHERE " + BaseDelegationTable.FieldDelegantId + " = '" + UserInfo.Id + "')";
            }
            sqlQuery += " ORDER BY " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldSortCode
                          + " ," + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            return DbHelper.Fill(sqlQuery);
        }
        #endregion

        #region public DataTable GetAllowList() 获取允许扮演列表
        /// <summary>
        /// 获取允许扮演列表
        /// </summary>
        /// <param name="search">查询</param>
        /// <returns>数据表</returns>
        public DataTable GetAllowList()
        {
            string sqlQuery = " SELECT " + BaseStaffTable.TableName + ".* "
                            + "," + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldFullName + " AS DepartmentFullName "
                            + "," + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " AS DutyFullName "
                            + "," + "OT.RoleFullName "
                            + " FROM " + BaseStaffTable.TableName
                            + "      LEFT OUTER JOIN " + BaseOrganizeTable.TableName + " ON " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldId + " = " + BaseStaffTable.FieldDepartmentId
                            + "      LEFT OUTER JOIN " + BaseItemDetailsTable.TableName + " ON  " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldId + " = " + BaseStaffTable.FieldDutyId
                            + "      LEFT OUTER JOIN " + "(SELECT " + BaseUserTable.TableName + "." + BaseUserTable.FieldId
                                                                + "," + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldItemName + " AS RoleFullName "
                                                         + " FROM " + BaseUserTable.TableName + "," + BaseItemDetailsTable.TableName
                                                         + " WHERE " + BaseUserTable.TableName + "." + BaseUserTable.FieldRoleId + " = " + BaseItemDetailsTable.TableName + "." + BaseItemDetailsTable.FieldId + ") OT "
                            + "                  ON " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldId + " = OT.ID "
                            + " WHERE (" + BaseStaffTable.TableName + ".ID <> '" + UserInfo.Id + "')";
            // 获得允许扮演的范围
            // if (!UserInfo.IsAdministrator)
            // {
            sqlQuery += " AND " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldId + " IN (SELECT " + BaseDelegationTable.FieldDelegantId
                                                              + " FROM " + BaseDelegationTable.TableName
                                                              + " WHERE " + BaseDelegationTable.FieldConsignerId + "= '" + UserInfo.Id + "')";
            // }
            sqlQuery += " ORDER BY " + BaseOrganizeTable.TableName + "." + BaseOrganizeTable.FieldSortCode
                      + " ," + BaseStaffTable.TableName + "." + BaseStaffTable.FieldSortCode;
            return DbHelper.Fill(sqlQuery);
        }
        #endregion
    }
}
